package com.longge;

import java.util.ArrayList;
import java.util.Arrays;

public class MaxSatisfaction {
    public int maxSatisfaction(int[] satisfaction) {

        Arrays.sort(satisfaction);
        int ans = 0, cur = 0;
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = satisfaction.length - 1; i >= 0; i--) {
            if (cur + satisfaction[i] > 0) {
                list.add(satisfaction[i]);
                cur += satisfaction[i];
            }
        }
        for (int i = 0; i < list.size(); i++) {
            ans+=(list.size()-i)*list.get(i);
        }
        return ans;
    }
}
